class Article < ActiveRecord::Base
  # day la tang model, hien em chua dua cac rang buoc voi ham can thiet o day, dang sai ham trong lop Base cua ruby on rails
  # vi du em cho article thuoc ve 1 user thi khai bao nhu sau
  validates :title, :presence => true
  validates :body, :presence => true
  
  belongs_to :event
  belongs_to :user
  belongs_to :article_status
  belongs_to :channel
  
  has_many :article_histories, :dependent => :destroy
  has_many :article_approvals, :dependent => :destroy
  
  has_one :image
  
  def self.filter( filters = {} )
	@where = ''

	if filters[:text] && !filters[:text].empty?
		@where += " (title LIKE '%#{ filters[:text] }%' OR" +
					" body LIKE '%#{ filters[:text] }%'" +					
					") AND"
	end
	
	if filters[:user_id] && !filters[:user_id].empty?
		@where += " user_id = '#{ filters[:user_id] }' AND"
	end
	
	if filters[:article_status_id] && !filters[:article_status_id].empty?
		@where += " article_status_id = '#{ filters[:article_status_id] }' AND"
	end
	
	if filters[:channel_id] && !filters[:channel_id].empty?
		@where += " channel_id = '#{ filters[:channel_id] }' AND"
	end
	
	if !@where.empty?
		@where += " 1=1"
	end
	
	self.where(@where)
  end
  
  def self.reporter_articles_list(user, filters = {})
	@where = "user_id = '#{ user.id }' AND"

	if filters[:text] && !filters[:text].empty?
		@where += " (title LIKE '%#{ filters[:text] }%' OR" +
					" body LIKE '%#{ filters[:text] }%'" +					
					") AND"
	end
	
	if filters[:article_status_id] && !filters[:article_status_id].empty?
		@where += " article_status_id = '#{ filters[:article_status_id] }' AND"
	end
	
	if filters[:channel_id] && !filters[:channel_id].empty?
		@where += " channel_id = '#{ filters[:channel_id] }' AND"
	end

	if !@where.empty?
		@where += " 1=1"
	end
	
	self.find(:all, :order => "articles.updated_at DESC", :conditions => @where)
  end
  
  def self.articles_status_table(filters = {})
    @where = ""
    
    if filters[:text] && !filters[:text].empty?
		@where += " (title LIKE '%#{ filters[:text] }%' OR" +
					" body LIKE '%#{ filters[:text] }%'" +					
					") AND"
    end
    
    if filters[:article_status_id] && !filters[:article_status_id].empty?
		@where += " article_status_id = '#{ filters[:article_status_id] }' AND"
    end
    
    if filters[:user_id] && !filters[:user_id].empty?
		@where += " user_id = '#{ filters[:user_id] }' AND"
    end

    if !@where.empty?
	    @where += " 1=1"
    end
    
    self.find(:all, :order => "articles.updated_at DESC", :conditions => @where)
  end
  
  def self.articles_approving
    Article.where("article_status_id = #{ArticleStatus.get("APPROVING").id}")
  end
  
  def self.articles_new
    Article.where("article_status_id = #{ArticleStatus.get("NEW").id}")
  end
  
  def save_history(username, remote_ip = '')
    article_histories.create(:title => title, :body => body, :username => username, :ip_address => remote_ip)
  end
  
  def reporter_edit?(current_user)
    article_status != ArticleStatus.get('APPROVING') && article_status != ArticleStatus.get('APPROVED') && user == current_user
  end
  
  def reporter_approving?
    article_status == ArticleStatus.get('APPROVING')
  end
  
end
